import 'package:flutter/foundation.dart';
import 'package:web_socket_channel/io.dart';
import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class WebSocketDemo extends StatefulWidget {

	String title;
	WebSocketChannel channel;

	WebSocketDemo() {
		this.channel = new IOWebSocketChannel.connect('ws://echo.websocket.org');
	}

	@override
	State<StatefulWidget> createState() {
		return WebSocketDemoState();
	}
}

class WebSocketDemoState extends State<WebSocketDemo> {
	TextEditingController _controller = new TextEditingController();

	@override
	Widget build(BuildContext context) {
		return new Scaffold(
			appBar: new AppBar(
				title: new Text("Websocket Demo"),
			),
			body: new Padding(
				padding: const EdgeInsets.all(20.0),
				child: new Column(
					crossAxisAlignment: CrossAxisAlignment.start,
					children: <Widget>[
						new Form(
							child: new TextFormField(
								controller: _controller,
								decoration: new InputDecoration(labelText: 'Send a message'),
							),
						),
						new StreamBuilder(
							stream: widget.channel.stream,
							builder: (context, snapshot) {
								return new Padding(
									padding: const EdgeInsets.symmetric(vertical: 24.0),
									child: new Text(snapshot.hasData ? '${snapshot.data}' : ''),
								);
							},
						)
					],
				),
			),
			floatingActionButton: new FloatingActionButton(
				onPressed: _sendMessage,
				tooltip: 'Send message',
				child: new Icon(Icons.send),
			), // This trailing comma makes auto-formatting nicer for build methods.
		);
	}

	void _sendMessage() {
		if (_controller.text.isNotEmpty) {
			widget.channel.sink.add(_controller.text);
		}
	}

	@override
	void dispose() {
		widget.channel.sink.close();
		super.dispose();
	}
}